/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Data.Event;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;

/**
 *
 * @author NGOCLINH-PC
 */
public class UpdateEvent extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    private final String UPLOAD_DIRECTORY = "C:\\Users\\NGOCLINH-PC\\Desktop\\nlpp263\\prj\\nlpp\\web\\uploads";
    String fullnameimage = "";

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, FileUploadException, Exception {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            int id = 0;
            int Category = 0;
            String eventname = null;
            String speaker = null;
            String address = null;
            String description = null;
            String content = null;
            float price = 0;
            String timeevent = null;
            String deadline = null;
            String fimage = null;
            int stt = 0;

            if (ServletFileUpload.isMultipartContent(request)) {
                try {

                    List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
                    for (FileItem item : multiparts) {
                        if (!item.isFormField()) {
                            String name = new File(item.getName()).getName();
                            item.write(new File(UPLOAD_DIRECTORY + File.separator + name));
                            //fullnameimage = name;
                            fimage = name;
                        } else {

                            if (item.getFieldName().equals("txtEventID")) {
                                id = Integer.parseInt(item.getString());
                            }
                            if (item.getFieldName().equals("txtCategory")) {
                                Category = Integer.parseInt(item.getString());
                            }
                            if (item.getFieldName().equals("txtEventname")) {
                                eventname = item.getString();
                            }
                            if (item.getFieldName().equals("txtSpeakers")) {
                                speaker = item.getString();
                            }
                            if (item.getFieldName().equals("txtAddress")) {
                                address = item.getString();
                            }
                            if (item.getFieldName().equals("txtDescription")) {
                                description = item.getString();
                            }
                            if (item.getFieldName().equals("txtContent")) {
                                content = item.getString();
                            }
                            if (item.getFieldName().equals("txtPrice")) {
                                price = Float.parseFloat(item.getString());
                            }
                            if (item.getFieldName().equals("txtTimeEvent")) {
                                timeevent = item.getString();
                            }
                            if (item.getFieldName().equals("txtDeadline")) {
                                deadline = item.getString();
                            }
                            if (item.getFieldName().equals("radio1")) {
                                stt = Integer.parseInt(item.getString());
                            }
                            if (item.getFieldName().equals("txturlanh")) {
                                fimage = item.getString();
                            }
                        }
                    }
                } catch (Exception ex) {
                    //request.setAttribute("message", "File Upload Failed due to " + ex);
                }
            }

            Event.Eupdate(id, eventname, speaker, address, description, content, price, timeevent, deadline, fimage, stt, Category);
            response.sendRedirect("/nlpp/admin/index.jsp?page=Event");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (FileUploadException ex) {
            Logger.getLogger(UpdateEvent.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(UpdateEvent.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (FileUploadException ex) {
            Logger.getLogger(UpdateEvent.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(UpdateEvent.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
